#pragma once
#include <memory>
#include <mutex>
#include <map>
#include "frame_common.h"
#include "AVFrame.h"
#include "Pal/AudioDec.h"

namespace Media{

    class ADecoder:public std::enable_shared_from_this<ADecoder>{
    public:
        using Ptr = std::shared_ptr<ADecoder>;
        using FrameConsumerId = uint32_t;
        ADecoder(int channel);
        ~ADecoder();

        bool Start();
        bool Stop();
        
        bool InputFrame(const FrameCommon::Frame::Ptr& frame);
        FrameConsumerId AddConsumer(std::function<void (const AVFrame::Ptr&)> consumer);
        bool RemoveConsumer(FrameConsumerId id);

        int GetChannel();
        void ReleaseFrame(void *frame);
    private:
        void frameProc(void *frame);

        std::map<FrameConsumerId, std::function<void (const AVFrame::Ptr&)>> m_frameConsumers;
        uint32_t m_frameConsumerIndex;
        std::recursive_mutex m_mux;
        AudioDecoder *m_audioDecoder;
        AudioDecFormat	m_format;

        int m_channel;
    };
}